#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

// #define Myself 1

// 分割符
const std::string blank_space_sep = " ";
const std::string protocol_sep = "\n";

// 添加报头
// "len"\n"x op y"\n
std::string Encode(std::string &content)
{
    std::string package = std::to_string(content.size());
    package += protocol_sep;
    package += content;
    package += protocol_sep;

    return package;
}

// 解析报头
// "len"\n"x op y"\n -> "x op y"
bool Decode(std::string &package, std::string *content)
{
    std::size_t pos = package.find(protocol_sep);
    if (pos == std::string::npos)
        return false;

    std::string len_str = package.substr(0, pos);
    std::size_t len = stoi(len_str);

    // 判断报头是否符合要求
    std::size_t total_len = len + len_str.size() + 2;
    if (package.size() < total_len)
        return false;

    // std::cout << "移除报文成功" << std::endl;
    *content = package.substr(pos + 1, len);
    package.erase(0, total_len);
    return true;
}

class Request
{
public:
    Request(int data1, int data2, char oper)
        : _x(data1), _y(data2), _op(oper)
    {
    }
    Request()
    {
    }
    // 构建报文有效载荷
    // struct -> string,"x op y"
    bool Serialize(std::string *out)
    {
#ifdef Myself
        std::string s = std::to_string(_x);
        s += blank_space_sep;
        s += _op;
        s += blank_space_sep;
        s += std::to_string(_y);

        *out = s;
        return true;
#else
        // 序列化
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["op"] = _op;
        Json::FastWriter w;
        *out = w.write(root);
        return true;

#endif
    }

    // 提取有效载荷，反序列化
    // "x op y" -> struct
    bool Deserialize(const std::string &in)
    {
#ifdef Myself
        // 提取左边
        size_t left = in.find(blank_space_sep);
        if (left == std::string::npos)
            return false;
        std::string part_x = in.substr(0, left);
        // 提取右边
        size_t right = in.rfind(blank_space_sep);
        if (right == std::string::npos)
            return false;
        std::string part_y = in.substr(right + 1);
        // 进行判断
        if (left + 2 != right)
            return false;
        // 填充结构体
        _x = stoi(part_x);
        _y = stoi(part_y);
        _op = in[left + 1];
        return true;
#else
        Json::Value root;
        Json::Reader rd;
        rd.parse(in,root);
        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _op = root["op"].asInt();
        return true;

#endif
    }

    void DebugPrint()
    {
        std::cout << "新请求构建完成:  " << _x << _op << _y << "=?" << std::endl;
    }

public:
    int _x;
    int _y;
    char _op;
};

class Response
{
public:
    Response(int res, int code)
        : _result(res), _code(code)
    {
    }
    Response()
    {
    }
    // 构建报文有效载荷
    // struct -> string,"_result _code"
    bool Serialize(std::string *out)
    {
#ifdef Myself
        std::string s = std::to_string(_result);
        s += blank_space_sep;
        s += std::to_string(_code);

        *out = s;
        return true;

#else
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;
        Json::FastWriter w;
        *out = w.write(root);
        return true;
#endif
    }
    // 反序列化
    // string,"_result _code" -> struct
    bool Deserialize(const std::string &in)
    {
#ifdef Myself
        std::size_t res = in.find(blank_space_sep);
        if (res == std::string::npos)
            return false;
        std::string left = in.substr(0, res);
        std::string right = in.substr(res + 1);

        _result = std::stoi(left);
        _code = std::stoi(right);
        return true;

#else
        Json::Value root;
        Json::Reader rd;
        rd.parse(in,root);
        _result = root["result"].asInt();
        _code = root["code"].asInt();

        return true;
#endif
    }

    void DebugPrint()
    {
        std::cout << "结果响应完成, result: " << _result << ", code: " << _code << std::endl;
    }

public:
    int _result;
    int _code;
};
